import { reactive, ref } from 'vue'

import { useMultipleTaskData } from '@/stores/multipleTaskData.js'
import { useSimpleTaskData } from '@/stores/simpleTaskData.js'

const multipleTaskData = useMultipleTaskData()

const simpleTaskData = useSimpleTaskData()

// 处理消息类型为0的处理函数
export default function initHandler() {
  // 初始化socket连接时,保存python发送的页面渲染数据
  const algorithms_data = ref([])
  const problems_data = ref([])
  const indicators = ref([])
  // 将保存的algorithms_data等进行形式上的转换，方便页面渲染
  // 将所有的算法和问题分别保存到两个对象中
  const algorithm_list = reactive({})
  const problem_list = reactive({})
  // 处理数据
  const handleInitMsg = (result) => {
    algorithms_data.value = result.algorithms_data
    problems_data.value = result.problems_data
    // ??
    multipleTaskData.indicators = JSON.parse(JSON.stringify(result.indicators))
    multipleTaskData.indicators['num of run'] = { name: 'num of run' }
    //
    simpleTaskData.indicators = JSON.parse(JSON.stringify(result.indicators))
    simpleTaskData.indicators['obj'] = { name: 'obj' }
    simpleTaskData.indicators['dec'] = { name: 'dec' }
    //
    getDataList(algorithm_list, result.algorithms_data)
    getDataList(problem_list, result.problems_data)
  }

// 将算法或问题名字作为key，其他信息作为value
// {name:{name:'LSMOP1',cls'....',params:[]},}
  const getDataList = (list, data) => {
    for (const item in data) {
      for (let i = 0; i < data[item].length; i++) {
        list[data[item][i].name] = data[item][i]
      }
    }
  }
  return {
    algorithms_data,
    problems_data,
    algorithm_list,
    indicators,
    problem_list,
    handleInitMsg
  }
}

